package com.three.iot.rest.controller.admin;

import com.three.iot.api.ProductAttrService;
import com.three.iot.commons.dto.ProductAttrCondDto;
import com.three.iot.commons.dto.ProductAttrCreateDto;
import com.three.iot.commons.dto.ProductAttrUpadateDto;
import com.three.iot.commons.enums.Result;
import com.three.iot.commons.enums.ResultCode;
import com.three.iot.rest.utils.RequestUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author luiz
 * @Title: ProductAttrController
 * @ProjectName iot-server
 * @Description: TODO
 * @date 2019-06-15 11:44
 */
@RestController
@RequestMapping("/admin/productAttr")
@Api(tags = "产品属性信息api")
public class ProductAttrController {
    private static final Logger logger = LoggerFactory.getLogger(ProductAttrController.class);
    @Resource
    private ProductAttrService productAtrrService;
    /**
     * 根据产品密钥查询属性列表
     */
    @PostMapping("/findAttrPage")
    @ApiOperation(value = "根据产品密钥查询属性列表", notes = "根据产品密钥查询属性列表")
    public Result listPage(@RequestBody ProductAttrCondDto productAttrCondDto) {
        try {
            return productAtrrService.listPage(productAttrCondDto);
        } catch (Exception e) {
            logger.error("根据产品密钥查询属性列表异常:{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    /**
     * 新增产品属性
     *
     */
    @PostMapping("/create")
    @ApiOperation(value = "新增产品属性", notes = "新增产品属性")
    public Result create(@RequestBody ProductAttrCreateDto productAttrCreateDto, HttpServletRequest request) {
        try {
            String userNo= RequestUtils.getCurrentUserNo(request);
            logger.info("新增产品属性，操作人:{}",userNo);
            productAttrCreateDto.setOperUserNo(userNo);
            productAtrrService.create(productAttrCreateDto);
            return Result.newSuccess();
        } catch (Exception e) {
            logger.error("新增产品属性失败:{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    /**
     * 修改产品属性
     *
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改产品属性", notes = "修改产品属性")
    public Result update(@RequestBody ProductAttrUpadateDto productAttrUpadateDto, HttpServletRequest request) {
        try {
            String userNo= RequestUtils.getCurrentUserNo(request);
            logger.info("修改产品属性，操作人:{}",userNo);
            productAttrUpadateDto.setOperUserNo(userNo);
            productAtrrService.update(productAttrUpadateDto);
            return Result.newSuccess();
        } catch (Exception e) {
            logger.error("修改产品属性失败:{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    /**
     * 删除产品属性
     *
     */
    @PostMapping("/deleteById/{id}")
    @ApiOperation(value = "删除产品属性", notes = "删除产品属性")
    public Result update(@PathVariable("id")String id, HttpServletRequest request) {
        try {
            String userNo= RequestUtils.getCurrentUserNo(request);
            logger.info("删除产品属性，操作人:{}",userNo);
            productAtrrService.delete(id);
            return Result.newSuccess();
        } catch (Exception e) {
            logger.error("删除产品属性失败:{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

}
